<html>
<head><meta charset="utf-8"><title>const fn Box&lt;T, A&gt;::new(v) · t-libs/wg-allocators · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/index.html">t-libs/wg-allocators</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html">const fn Box&lt;T, A&gt;::new(v)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="165420000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165420000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165420000">(May 11 2019 at 17:03)</a>:</h4>
<p>Hello, i've got a question: What will it take to make Box&lt;T, A&gt;::new a const fn?</p>



<a name="165543742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165543742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Ericson <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165543742">(May 13 2019 at 16:20)</a>:</h4>
<p>That would probably require at the very least <code>const fn</code> trait methods and a separate <code>AllocConst: Alloc</code>, <code>AllocConst::alloc</code>, and <code>Box::new_cost</code></p>



<a name="165629668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165629668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165629668">(May 14 2019 at 15:00)</a>:</h4>
<p><span class="user-mention" data-user-id="116458">@Charles Lew</span> you can't</p>



<a name="165629752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165629752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165629752">(May 14 2019 at 15:01)</a>:</h4>
<p>you need a new method, e.g., </p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">const</span><span class="w"> </span><span class="n">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">const_new</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>at least with <a href="https://github.com/rust-rfcs/const-eval/pull/8" target="_blank" title="https://github.com/rust-rfcs/const-eval/pull/8">https://github.com/rust-rfcs/const-eval/pull/8</a></p>



<a name="165629769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165629769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165629769">(May 14 2019 at 15:01)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span> ^^^</p>



<a name="165629875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165629875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165629875">(May 14 2019 at 15:02)</a>:</h4>
<p>In</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p><code>A</code> might be any type that implements <code>Alloc</code>, independently of whether the impl of <code>Alloc</code> for <code>A</code> is <code>const</code> or not.</p>



<a name="165630029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165630029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165630029">(May 14 2019 at 15:04)</a>:</h4>
<p>To allow <code>new</code> to be <code>const fn</code> only when <code>A: const Alloc</code> we would need some form of negative reasoning, such that  these two impls do not overlap:</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">Alloc</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">!</span><span class="k">const</span><span class="w"> </span><span class="n">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">...</span><span class="w"> </span><span class="n">non</span><span class="w"> </span><span class="k">const</span><span class="p">...;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">const</span><span class="w"> </span><span class="n">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">...;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>I don't think this feature is being pursued anywhere.</p>



<a name="165634123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165634123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165634123">(May 14 2019 at 15:53)</a>:</h4>
<p>ok. Well i guess maybe people will want const fn Box::new() with system allocator at some point... so, with the ability to specify an  extra allocator (handle), will we lose it (maybe permanently)?</p>



<a name="165634634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165634634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165634634">(May 14 2019 at 15:58)</a>:</h4>
<p>with <a href="https://github.com/rust-lang/rfcs/pull/2632" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2632">https://github.com/rust-lang/rfcs/pull/2632</a></p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165634718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165634718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165634718">(May 14 2019 at 15:59)</a>:</h4>
<p>should work, because <code>const fn new</code> will require <code>A: const Alloc</code> if and only if called from a const context</p>



<a name="165634740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165634740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165634740">(May 14 2019 at 15:59)</a>:</h4>
<p>there is no <code>const Alloc</code> syntax for bounds</p>



<a name="165634769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165634769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165634769">(May 14 2019 at 15:59)</a>:</h4>
<p>if you want to opt-out of the <code>const Alloc</code> requirement even in const contexts, you need to use <code>A: ?const Alloc</code></p>



<a name="165670042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/const%20fn%20Box%3CT%2C%20A%3E%3A%3Anew%28v%29/near/165670042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mike Hommey <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/const.20fn.20Box.3CT.2C.20A.3E.3A.3Anew(v).html#165670042">(May 14 2019 at 22:57)</a>:</h4>
<p>an <code>Alloc</code> provided that allocates in <code>.data</code> (thus... provided by the compiler itself) could be useful for this.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>